package org.mirrentools.remoteattendance.options;

import io.vertx.core.json.JsonObject;
import org.mirrentools.remoteattendance.common.StringUtil;

import java.util.Objects;

/**
 * 系统的配置信息
 */
public class Options {
  private Options() {
  }

  /**
   * 创建一个配置实例
   *
   * @param config 配置信息
   * @return 返回实例
   */
  public static Options from(JsonObject config) {
    Objects.requireNonNull(config);
    Options options = new Options();
    options.setLogger(config.getBoolean("logger"));
    options.setHost(config.getString("host"));
    options.setMaxWebClientSize(StringUtil.getInteger(config.getInteger("maxWebClientSize"), 30));
    float pixel = StringUtil.getfloat(config.getFloat("pixel"), 0.4F);
    if (pixel < 0.1 || pixel > 1.0) {
      pixel = 0.4F;
    }
    options.setPixel(pixel);
    int interval = StringUtil.getInteger(config.getInteger("interval"), 0);
    if (interval < 1) {
      interval = 45;
    }
    options.setInterval(interval);
    options.setUid(config.getString("uid"));
    options.setToken(config.getString("token"));
    System.out.println("init options:" + options);
    return options;
  }

  /**
   * 是否打印日志
   */
  private boolean logger;
  /**
   * 服务器的主机地址
   */
  private String host;
  /**
   * 截图的清晰度
   */
  private float pixel;
  /**
   * 截图的间隔
   */
  private int interval;
  /**
   * 客户端的连接数
   */
  private int maxWebClientSize;
  /**
   * 用户的id
   */
  private String uid;
  /**
   * 用户的token
   */
  private String token;

  private Options setMaxWebClientSize(int maxWebClientSize) {
    this.maxWebClientSize = maxWebClientSize;
    return this;
  }

  private Options setLogger(boolean logger) {
    this.logger = logger;
    return this;
  }

  private Options setHost(String host) {
    this.host = host;
    return this;
  }

  private Options setUid(String uid) {
    this.uid = uid;
    return this;
  }

  private Options setToken(String token) {
    this.token = token;
    return this;
  }

  private Options setPixel(float pixel) {
    this.pixel = pixel;
    return this;
  }

  private Options setInterval(int interval) {
    this.interval = interval;
    return this;
  }

  /**
   * 是否打印日志
   */
  public boolean logger() {
    return logger;
  }

  /**
   * 获取客户端连接数
   */
  public int maxWebClientSize() {
    return maxWebClientSize;
  }

  /**
   * 截图的清晰度
   */
  public float pixel() {
    if (pixel < 0.1 || pixel > 1.0) {
      return 0.5F;
    }
    return pixel;
  }

  /**
   * 截图的间隔
   */
  public int interval() {
    if (interval==0){
      return 30;
    }
    return interval;
  }

  /**
   * 获取主机地址
   */
  public String host() {
    return host;
  }

  /**
   * 获取用户的id
   */
  public String uid() {
    return uid;
  }

  /**
   * 获取用户的token
   */
  public String token() {
    return token;
  }

  @Override
  public String toString() {
    return "Options{" +
            "logger=" + logger +
            ", host='" + host + '\'' +
            ", pixel=" + pixel +
            ", interval=" + interval +
            ", maxWebClientSize=" + maxWebClientSize +
            ", uid='" + uid + '\'' +
            ", token='" + token + '\'' +
            '}';
  }
}
